package com.cskaoyan.mall.controller.mall;

import com.cskaoyan.mall.model.Goods;
import com.cskaoyan.mall.model.Result;
import com.cskaoyan.mall.model.bo.mall.AskGoodsMsgBO;
import com.cskaoyan.mall.model.vo.mall.*;
import com.cskaoyan.mall.service.admin.GoodsService;
import com.cskaoyan.mall.service.admin.GoodsServiceImpl;
import com.cskaoyan.mall.service.mall.MallGoodsService;
import com.cskaoyan.mall.service.mall.MallGoodsServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/api/mall/goods/*")
public class MallGoodsServlet extends HttpServlet {
    private GoodsService goodsService = new GoodsServiceImpl();
    private MallGoodsService mallGoodsService = new MallGoodsServiceImpl();
    private Gson gson = new Gson();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("dopost");
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/goods/","");
        if ("askGoodsMsg".equals(action)) {
            askGoodsMsg(request,response);
        }
    }
    /**
     * 发送咨询
     * @param request
     * @param response
     */
    private void askGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        //对请求报文进行处理
        AskGoodsMsgBO msg = gson.fromJson(requestBody, AskGoodsMsgBO.class);
        int code = mallGoodsService.askGoodsMsg(msg);;
        if (code == 200) {
            response.getWriter().println(gson.toJson(Result.ok()));
            return;
        }
        response.getWriter().println(gson.toJson(Result.error("发送商品消息失败")));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/mall/goods/", "");
        if("getGoodsByType".equals(action)){
            getGoodsByType(request, response);
        }else if ("searchGoods".equals(action)) {
            searchGoods(request,response);
        }else if ("getGoodsInfo".equals(action)) {
            getGoodsInfo(request,response);
        }else if ("getGoodsMsg".equals(action)) {
            getGoodsMsg(request,response);
        }else if ("getGoodsComment".equals(action)) {
            getGoodsComment(request,response);
        }
    }

    /**
     * 获取商品的评论列表
     * 传来的是一个id
     * @param request
     * @param response
     */
    private void getGoodsComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("goodsId");
        GetGoodsCommentVO commentVO = mallGoodsService.getGoodsComment(id);
        Gson gson1 = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        response.getWriter().println(gson1.toJson(Result.ok(commentVO)));
    }

    /**
     * 查看商品的消息
     * {"code":0,"data":
     * [{"id":302,
     * "content":"AA",
     * "asker":"zmyy",
     * "time":"2020-08-04 17:49:56",
     * "reply":{"content":"你买单吧","time":"2020-08-05 17:34:07"}},
     *
     * {"code":0,"data":
     * [{"id":1,
     * "content":"这件衣服质量怎么样",
     * "asker":"Wanger",
     * "time":"Aug 11, 2020 8:52:51 PM",
     * "reply":{"content":"好像还可以","time":"Aug 12, 2020 2:31:58 PM"}},
     * @param request
     * @param response
     */
    private void getGoodsMsg(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        List<GetGoodsMsgVO> goodsMsgList = mallGoodsService.getGoodsMsg(id);
        Gson gson1 = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        response.getWriter().println(gson1.toJson(Result.ok(goodsMsgList)));
    }

    /**
     *
     * @param request
     * @param response
     */
    private void getGoodsInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        GetGoodsInfoVO goodsInfo = mallGoodsService.getGoodsInfo(id);
        response.getWriter().println(gson.toJson(Result.ok(goodsInfo)));
    }

    /**
     * 查找对应的goods
     * "data":[{"id":476,
     * "img":"http://115.29.141.32:8084/static/image/1597044682766u=1556642291,1559320101&fm=26&gp=0.jpg",
     * "name":"西服套装2",
     * "price":333.0,"
     * typeId":191},
     * @param request
     * @param response
     */
    private void searchGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String keyword = request.getParameter("keyword");
        List<SearchGoodsVO> searchGoodsVOS = mallGoodsService.searchGoods(keyword);
        response.getWriter().println(gson.toJson(Result.ok(searchGoodsVOS)));
    }

    /**
     * 获取那个每个类目下的商品
     * {"code":0,"data":[{
     * "id":476,
     * "img":"http://115.29.141.32:8084/static/image/1597044682766u=1556642291,1559320101&fm=26&gp=0.jpg",
     * "name":"西服套装2",
     * "price":333.0,
     * "typeId":191,
     * "stockNum":238}
     *     private Integer id;
     *     private String name;
     *     private String img;
     *     private Integer typeId;
     *
     *     private Double price;
     *     private Integer stockNum;
     *     private String desc;
     * @param response
     */
    private void getGoodsByType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String typeId = request.getParameter("typeId");
        List<Goods> goodsList = goodsService.getGoodsByType(typeId);
        GetGoodsByTypeVO getGoodsByTypeVOS = null;
        List<GetGoodsByTypeVO> getGoodsByTypeVOList = new ArrayList<GetGoodsByTypeVO>();
        //GetGoodsByTypeVO(Integer id, String name, String img, Integer typeId, Double price, Integer stockNum)
        for (Goods goods : goodsList) {
            getGoodsByTypeVOS = new GetGoodsByTypeVO(goods.getId(),goods.getName(),goods.getImg(),goods.getTypeId(),goods.getPrice(),goods.getStockNum());
            getGoodsByTypeVOList.add(getGoodsByTypeVOS);
        }
        response.getWriter().println(gson.toJson(Result.ok(getGoodsByTypeVOList)));
    }
}
